﻿using System;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace AutoGuiHelp
{
    internal class GuiHelper
    {
        public enum RasterOps : uint
        {
            SRCCOPY     = 0x00CC0020,
            SRCPAINT    = 0x00EE0086,
            SRCAND      = 0x008800C6,
            SRCINVERT   = 0x00660046,
            SRCERASE    = 0x00440328,
            NOTSRCCOPY  = 0x00330008,
            NOTSRCERASE = 0x001100A6,
            MERGECOPY   = 0x00C000CA,
            MERGEPAINT  = 0x00BB0226,
            PATCOPY     = 0x00F00021,
            PATPAINT    = 0x00FB0A09,
            PATINVERT   = 0x005A0049,
            DSTINVERT   = 0x00550009,
            BLACKNESS   = 0x00000042,
            WHITENESS   = 0x00FF0062,
            CAPTUREBLT  = 0x40000000 //only if WinVer >= 5.0.0 (see wingdi.h)
        }

        delegate IntPtr GetWindowDCDelegate (IntPtr hWnd);
        delegate bool   BitBltDelegate      (IntPtr hObject, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hObjSource, int nXSrc, int nYSrc, RasterOps dwRop);

        class WIN32FUNCS
        {
            [DllImport("user32.dll")]
            public static extern IntPtr GetWindowDC(
                IntPtr      hWnd
            );

            [DllImport("gdi32.dll")]
            public static extern bool BitBlt(
                IntPtr      hObject,
                int         nXDest,
                int         nYDest,
                int         nWidth,
                int         nHeight,
                IntPtr      hObjSource,
                int         nXSrc,
                int         nYSrc,
                RasterOps   dwRop
            );
        }

        class WINCEFUNCS
        {
            [DllImport("coredll.dll")]
            public static extern IntPtr GetWindowDC(
                IntPtr      hWnd
            );

            [DllImport("coredll.dll")]
            public static extern bool BitBlt(
                IntPtr      hObject,
                int         nXDest,
                int         nYDest,
                int         nWidth,
                int         nHeight,
                IntPtr      hObjSource,
                int         nXSrc,
                int         nYSrc,
                RasterOps   dwRop
            );
        }
        
        public static Bitmap getControlImage (Control control)
        {
            GetWindowDCDelegate getWindowDC = Environment.OSVersion.Platform == PlatformID.WinCE ?
                (GetWindowDCDelegate) WINCEFUNCS.GetWindowDC : (GetWindowDCDelegate) WIN32FUNCS.GetWindowDC;

            BitBltDelegate bitBlt = Environment.OSVersion.Platform == PlatformID.WinCE ?
                (BitBltDelegate) WINCEFUNCS.BitBlt : (BitBltDelegate)WIN32FUNCS.BitBlt;

            Bitmap      bmp = new Bitmap (control.Width, control.Height);
            Graphics    g   = System.Drawing.Graphics.FromImage(bmp);
            IntPtr      dc1 = g.GetHdc ();
            IntPtr      dc2 = getWindowDC (control.Handle);
            bitBlt (dc1, 0, 0, control.Width, control.Height, dc2, 0, 0, RasterOps.SRCCOPY);
            g.ReleaseHdc(dc1);
            return bmp;
        }
    }
}

